********************************************************************
* 	Replication code for:
*	"Projection in the Face of Centrism: Voter Inferences About 
*	Candidates' Party Affiliation in Low-Information Contexts"
*	Political Psychology 2023, Anthony Kevins & Seonghui Lee
********************************************************************

set scheme plotplain, permanently 
net install grc1leg,from( http://www.stata.com/users/vwiggins/) 

use Kevins_Lee_PolPsy2023.dta, clear
use "$dataDir/Kevins_Lee_PolPsy2023.dta", clear

*==============================================================================
*	recoding, labelling, summary statistics
*==============================================================================
	
* assumed party affiliation of John: binary variables

	gen johnLab_uk = johnparty_uk==1  
	gen johnLibdem_uk = johnparty_uk==2 
	gen johnCons_uk = johnparty_uk==3
	gen johnGreen_uk = johnparty_uk==4
	gen johnOther_uk = johnparty_uk==5
	gen johnDK_uk = johnparty_uk==99
	gen johnDKother_uk = 1 if johnparty_uk==99|johnparty_uk==5
	recode johnLab_uk - johnDKother_uk (0 = .) if johnparty_uk == .
	
	gen johnNdp_cnd = johnparty_cnd==1  
	gen johnLib_cnd = johnparty_cnd==2 
	gen johnCons_cnd = johnparty_cnd==3
	gen johnGreen_cnd = johnparty_cnd==4
	gen johnOther_cnd = johnparty_cnd==5
	gen johnDK_cnd = johnparty_cnd==99
	gen johnDKother_cnd = 1 if johnparty_cnd==99|johnparty_cnd==5
	recode johnNdp_cnd - johnDKother_cnd (0 = .) if johnparty_cnd == .
	
	gen johnDem_us = johnparty_us==1
	gen johnRep_us = johnparty_us==2
	gen johnGreen_us = johnparty_us==3
	gen johnOther_us = johnparty_us==4
	gen johnDK_us = johnparty_us==999
	gen johnDKother_us = 1 if johnparty_us==999|johnparty_us==4
	recode johnDem_us - johnDKother_us (0 = .) if johnparty_us == .

* party identification using ptv (Canada, UK)

	* get the highest value of PTV
	egen highPTV_uk = rowmax(ptv_cons ptv_lab ptv_snp ptv_libdem) if cntry=="UK" // 
	* note which party the highest value belongs to
	foreach p in cons lab libdem snp {
		gen pid_`p'_uk = 0 if cntry=="UK"
		replace pid_`p'_uk = 1 if highPTV_uk == ptv_`p' 
		}
	* how many tied cases? note the number of parties sharing the highest ptv values
	egen numCLpartyPTV_uk = rowtotal(pid_*_uk) if cntry=="UK"
	* dealing with straightliners: drop them since ptvs for all parties are identical 
	gen STptv_uk = 1 if (ptv_cons==ptv_lab)&(ptv_lab==ptv_libdem)&(ptv_libdem==ptv_cons)&cntry=="UK"	
	recode pid_*_uk (1 0 =.) if STptv_uk==1	
	
	* do the same for Canada 
	egen highPTV_cnd = rowmax(ptv_lib ptv_cons ptv_ndp ptv_bq) if cntry=="Canada"
	foreach p in lib cons ndp bq {
		gen pid_`p'_cnd = 0  if cntry=="Canada"
		replace pid_`p'_cnd =1 if highPTV_cnd == ptv_`p'
		}
	egen numCLpartyPTV_cnd = rowtotal(pid_*_cnd) if cntry=="Canada"
	gen STptv_cnd = 1 if (ptv_cons==ptv_lib)&(ptv_lib==ptv_ndp)&(ptv_ndp==ptv_cons)&cntry=="Canada"	
	recode pid_*_cnd (1 0 =.) if STptv_cnd==1

* party identification (USA): 7 pt scale

	foreach i in dem rep ind {
		gen pid_`i'_us = 0 if cntry=="US"
	}
	replace pid_dem_us = 1 if cntry=="US" & (pid==1|pid==2|pid==3) 
	replace pid_rep_us = 1 if cntry=="US" & (pid==5|pid==6|pid==7) 
	replace pid_ind_us = 1 if cntry=="US" & pid==4
	
* projection

gen johnMyparty_cnd = 0 if cntry=="Canada" 
	replace johnMyparty_cnd = 1 if johnNdp_cnd==1&pid_ndp_cnd==1
	replace johnMyparty_cnd = 1 if johnCons_cnd==1&pid_cons_cnd==1
	replace johnMyparty_cnd = 1 if johnLib_cnd==1&pid_lib_cnd==1
gen johnMyparty_uk = 0 if cntry=="UK"
	replace johnMyparty_uk = 1 if johnLab_uk==1&pid_lab_uk==1
	replace johnMyparty_uk = 1 if johnCons_uk==1&pid_cons_uk==1
	replace johnMyparty_uk = 1 if johnLibdem_uk==1&pid_libdem_uk==1
gen johnMyparty_us = 0 if cntry=="US"
	replace johnMyparty_us = 1 if johnDem_us==1&pid_dem_us==1
	replace johnMyparty_us = 1 if johnRep_us==1&pid_rep_us==1

* ethnicity

	* UK & Canada
	forval i = 1/8 {
		replace rethnicity_`i'1 = rethnicity_`i'2 if rethnicity_`i'2!=.
		replace rethnicity_`i'1 = rethnicity_`i'3 if rethnicity_`i'3!=.
		replace rethnicity_`i'1 = rethnicity_`i'4 if rethnicity_`i'4!=.
	}
	recode rethnicity_*1 (.=0)
	
	gen white_uk = 0 if cntry=="UK"
		replace white_uk = 1 if cntry=="UK"&(rethnicity_11==1|rethnicity_21==1|rethnicity_31==1|rethnicity_41==1|rethnicity_51==1)
	gen nonwhite_uk = 0 if cntry=="UK"
		replace nonwhite_uk = 1 if cntry=="UK"&(rethnicity_61==1|rethnicity_71==1|rethnicity_81==1)
	gen mix_uk = 0 if cntry=="UK"
		replace mix_uk = 1 if cntry=="UK"&(white_uk==1&nonwhite_uk==1)
	replace white_uk = 0 if mix_uk==1
	replace nonwhite_uk = 0 if mix_uk==1
		
	gen white_cnd = 0 if cntry=="Canada"
		replace white_cnd = 1 if cntry=="Canada"&(rethnicity_11==1|rethnicity_21==1|rethnicity_41==1|rethnicity_51==1)
	gen nonwhite_cnd = 0 if cntry=="Canada"
		replace nonwhite_cnd = 1 if cntry=="Canada"&(rethnicity_31==1|rethnicity_61==1|rethnicity_71==1|rethnicity_81==1)
	gen mix_cnd = 0		
		replace mix_cnd = 1 if white_cnd==1&nonwhite_cnd==1	
	replace white_cnd = 0 if mix_cnd==1
	replace nonwhite_cnd = 0 if mix_cnd==1
	
	* USA
	split race, p(,) destring
	forval i = 1/6 {
		gen usrace`i' = 0 if cntry=="US"
	}
	forval i = 1/6 {
		forval j = 1/6 {
			replace usrace`j' = 1 if race`i'==`j'
		}
	}
	gen white_us = 0 if cntry=="US"
		replace white_us = 1 if cntry=="US" & usrace5==1
	gen nonwhite_us = 0 if cntry=="US"
		replace nonwhite_us = 1 if cntry=="US" & (usrace1==1|usrace2==1|usrace3==1|usrace4==1|usrace6==1)	
	gen mix_us = 0 if cntry=="US"
		replace mix_us = 1 if white_us==1&nonwhite_us==1
	replace white_us = 0 if mix_us == 1
	replace nonwhite_us = 0 if mix_us ==1
	
* class

	recode class (1=1) (2=2) (3 4 5 =3) if cntry=="UK"|cntry=="Canada", gen(classR3)
	tab classR3
/*	replace johnclass = johnclass1 if johnclass == .
		replace johnclass = johnclass2 if johnclass == .
		replace johnclass = johnclass3 if johnclass == .
		replace johnclass = johnclass4 if johnclass == .*/
	tab johnclass
	recode johnclass (1=1) (2=2) (3 4 = 3) (99=.) if cntry=="UK"|cntry=="Canada", gen(classJohn3)

	replace classR3 = 1 if cntry=="US"&(class==1|class==2)
	replace classR3 = 2 if cntry=="US"&class==3
	replace classR3 = 3 if cntry=="US"&(class==4|class==5)
	replace classJohn3 = 1 if cntry=="US"&(johnclass==1|johnclass==2)
	replace classJohn3 = 2 if cntry=="US"&johnclass==3
	replace classJohn3 = 3 if cntry=="US"&(johnclass==4|johnclass==5)

* ingroup-ness 

	gen johnMyclass = 0 
		replace johnMyclass = 1 if classJohn3 == classR3
	gen johnMyeth = 0 
	foreach i in cnd uk us {
		replace johnMyeth = 1 if white_`i' == 1 & tr_irish == 1
		replace johnMyeth = 1 if nonwhite_`i' == 1 & tr_korean == 1	
	}
	
* other shared characteristics 
gen pubSchool = 0
	replace pubSchool = 1 if rminority_11==1|rminority_12==1|rminority_13==1|rminority_14==1
	replace pubSchool = 1 if controls_public_school==1&cntry=="US"
gen immParent = 0
	replace immParent = 1 if rminority_21==1|rminority_22==1|rminority_23==1|rminority_24==1
	replace immParent = 1 if controls_immigrant_family==1&cntry=="US"
gen lrself = lrplace_self if cntry!="US"
	replace lrself = lrplace_self1 if lrself==.
	recode lrself 99 = .

* identify a sample for hypotheses testing: only include those who identify with a party
	capture drop sample
	gen sample = 1 if cntry=="UK"&(pid_cons_uk==1|pid_lab_uk==1|pid_libdem_uk==1|pid_snp_uk==1)& numCLpartyPTV_uk==1
	replace sample = 1 if cntry=="Canada"&(pid_cons_cnd==1|pid_lib_cnd==1|pid_ndp_cnd==1|pid_bq_cnd==1) & numCLpartyPTV_cnd==1
	replace sample = 1 if cntry=="US" & pid_ind_us==0

*--------------------------------
*	online appendix table A
*--------------------------------
bysort cntry: sum male age agecatUS class pubSchool immParent lrself libcon
sum white_* 

*--------------------------------
*	online appendix table B1
*--------------------------------
tab johnparty_cnd
tab johnparty_uk
tab johnparty_us

*==============================================================================
*	treatment effects; figure 1
*==============================================================================

* run 16 ttests and store estimates 

capture drop order cname pname gname
gen order = _n in 1/64
gen cname = ""
gen pname = ""
gen gname = ""
foreach i in mean sd n lo hi {
	capture drop `i'
	gen `i' = .
}

capture program drop mu1
program define mu1
	replace gname = "HC" in `1'
	replace mean = r(mu_1) in `1' 
	replace sd = r(sd_1) in `1' 
	replace n = r(N_1) in `1'
end
capture program drop mu2
program define mu2
	replace gname = "LC" in `1'
	replace mean = r(mu_2) in `1' 
	replace sd = r(sd_2) in `1' 
	replace n = r(N_2) in `1'
end
capture program drop mu1eth
program define mu1eth
	replace gname = "Irish" in `1'
	replace mean = r(mu_1) in `1' 
	replace sd = r(sd_1) in `1' 
	replace n = r(N_1) in `1'
end
capture program drop mu2eth
program define mu2eth
	replace gname = "Korean" in `1'
	replace mean = r(mu_2) in `1' 
	replace sd = r(sd_2) in `1' 
	replace n = r(N_2) in `1'
end

	local i = 1
	ttest johnNdp_cnd, by(tr_lc)
		replace pname = "NDP" in `i'
		mu1 `i'
		local i = `i'+1
		replace pname = "NDP" in `i'
		mu2 `i'
	local i = `i'+1		
	ttest johnLib_cnd, by(tr_lc)
		replace pname = "Lib" in `i'
		mu1 `i'
		local i = `i'+1
		replace pname = "Lib" in `i'
		mu2 `i'
	local i = `i'+1		
	ttest johnCons_cnd, by(tr_lc)
		replace pname = "Cons" in `i'
		mu1 `i'
		local i = `i'+1
		replace pname = "Cons" in `i'
		mu2 `i'
	local i = `i'+1		
	ttest johnLab_uk, by(tr_lc)
		replace pname = "Lab" in `i'
		mu1 `i'
		local i = `i'+1
		replace pname = "Lab" in `i'
		mu2 `i'
	local i = `i'+1		
	ttest johnLibdem_uk, by(tr_lc)
		replace pname = "LibDem" in `i'
		mu1 `i'
		local i = `i'+1
		replace pname = "LibDem" in `i'
		mu2 `i'
	local i = `i'+1		
	ttest johnCons_uk, by(tr_lc)
		replace pname = "Cons" in `i'
		mu1 `i'
		local i = `i'+1
		replace pname = "Cons" in `i'
		mu2 `i'
	local i = `i'+1		
	ttest johnDem_us, by(tr_lc)
		replace pname = "Dem" in `i'
		mu1 `i'
		local i = `i'+1
		replace pname = "Dem" in `i'
		mu2 `i'
	local i = `i'+1		
	ttest johnRep_us, by(tr_lc)
		replace pname = "Rep" in `i'
		mu1 `i'
		local i = `i'+1
		replace pname = "Rep" in `i'
		mu2 `i'
	local i = `i'+1		
	ttest johnNdp_cnd, by(tr_korean)
		replace pname = "NDP" in `i'
		mu1eth `i'
		local i = `i'+1
		replace pname = "NDP" in `i'
		mu2eth `i'
	local i = `i'+1		
	ttest johnLib_cnd, by(tr_korean)
		replace pname = "Lib" in `i'
		mu1eth `i'
		local i = `i'+1
		replace pname = "Lib" in `i'
		mu2eth `i'
	local i = `i'+1		
	ttest johnCons_cnd, by(tr_korean)
		replace pname = "Cons" in `i'
		mu1eth `i'
		local i = `i'+1
		replace pname = "Cons" in `i'
		mu2eth `i'
	local i = `i'+1		
	ttest johnLab_uk, by(tr_korean)
		replace pname = "Lab" in `i'
		mu1eth `i'
		local i = `i'+1
		replace pname = "Lab" in `i'
		mu2eth `i'
	local i = `i'+1		
	ttest johnLibdem_uk, by(tr_korean)
		replace pname = "LibDem" in `i'
		mu1eth `i'
		local i = `i'+1
		replace pname = "LibDem" in `i'
		mu2eth `i'
	local i = `i'+1		
	ttest johnCons_uk, by(tr_korean)
		replace pname = "Cons" in `i'
		mu1eth `i'
		local i = `i'+1
		replace pname = "Cons" in `i'
		mu2eth `i'
	local i = `i'+1		
	ttest johnDem_us, by(tr_korean)
		replace pname = "Dem" in `i'
		mu1eth `i'
		local i = `i'+1
		replace pname = "Dem" in `i'
		mu2eth `i'
	local i = `i'+1		
	ttest johnRep_us, by(tr_korean)
		replace pname = "Rep" in `i'
		mu1eth `i'
		local i = `i'+1
		replace pname = "Rep" in `i'
		mu2eth `i'
replace hi = mean + invttail(n-1, 0.085)*(sd/sqrt(n))
replace lo = mean - invttail(n-1, 0.085)*(sd/sqrt(n))
		
capture drop corder
gen corder = order 
	replace corder = corder - 16 if corder > 16
replace cname = "Canada" if corder>=1 & corder<=6 
replace cname = "UK" if corder>=7 & corder<=12
replace cname = "USA" if corder>=13 & corder<=16 

capture drop xax
gen xax = corder
	replace xax = xax - 6 if cname=="UK"
	replace xax = xax - 12 if cname=="USA"
recode xax 1=.8 2=1.2 3=2.8 4=3.2 5=4.8 6=5.2 


*--------------------------------
* figure 1 
*--------------------------------

* top panel

twoway (scatter mean xax if gname=="HC"&cname=="Canada", msymbol(circle) mcolor(gs6)) (scatter mean xax if gname=="LC"&cname=="Canada", msymbol(circle_hollow) mcolor(gs6)) (rspike hi lo xax if gname=="HC"&cname=="Canada", lcolor(gs6)) (rspike hi lo xax if gname=="LC"&cname=="Canada", lcolor(gs6)), xtitle(" ", size(medium) margin(small)) ylabel(.1(.1).4) legend(off) xlabel(0 " " 1 "NDP" 3 "Lib" 5 "Cons" 6 " ") title("Canada") name(trlc_cnd, replace) 
	
twoway (scatter mean xax if gname=="HC"&cname=="UK", msymbol(circle) mcolor(gs6)) (scatter mean xax if gname=="LC"&cname=="UK", msymbol(circle_hollow) mcolor(gs6)) (rspike hi lo xax if gname=="HC"&cname=="UK", lcolor(gs6)) (rspike hi lo xax if gname=="LC"&cname=="UK", lcolor(gs6)), xtitle("Assumed Party Affiliation", size(medium) margin(small)) ylabel(.1(.1).4) legend(order(1 "Higher class" 2 "Lower class") rows(1) position(6) region(margin(zero))) xlabel(0 " " 1 "Lab" 3 "LibDem" 5 "Cons" 6 " ") title("UK") name(trlc_uk, replace)
	
twoway (scatter mean xax if gname=="HC"&cname=="USA", msymbol(circle) mcolor(gs6)) (scatter mean xax if gname=="LC"&cname=="USA", msymbol(circle_hollow) mcolor(gs6)) (rspike hi lo xax if gname=="HC"&cname=="USA", lcolor(gs6)) (rspike hi lo xax if gname=="LC"&cname=="USA", lcolor(gs6)), xtitle(" ", size(medium) margin(small)) ylabel(.1(.1).4) legend(off) xlabel(0 " " 1 "Dem" 3 "Rep" 4 " ") title("USA") name(trlc_us, replace)
	
grc1leg2 trlc_cnd trlc_uk trlc_us, xsize(7) ysize(2.5) rows(1) scale(1.8) ///
	imargin(small) graphregion(margin(zero)) plotregion(margin(zero)) ///
	legendfrom(trlc_uk) l1(Proportion, size(small)) title("Social Class Treatment") 
graph export fig1_top.png
	
* bottom panel

twoway (scatter mean xax if gname=="Irish"&cname=="Canada", msymbol(circle) mcolor(gs6)) (scatter mean xax if gname=="Korean"&cname=="Canada", msymbol(circle_hollow) mcolor(gs6)) (rspike hi lo xax if gname=="Irish"&cname=="Canada", lcolor(gs6)) (rspike hi lo xax if gname=="Korean"&cname=="Canada", lcolor(gs6)), xtitle(" ", size(medium) margin(small)) xlabel(0 " " 1 "NDP" 3 "Lib" 5 "Cons" 6 " ") legend(off) title("Canada") ylabel(.1(.1).4) name(trkor_cnd, replace) 
	
twoway (scatter mean xax if gname=="Irish"&cname=="UK", msymbol(circle) mcolor(gs6)) (scatter mean xax if gname=="Korean"&cname=="UK", msymbol(circle_hollow) mcolor(gs6)) (rspike hi lo xax if gname=="Irish"&cname=="UK", lcolor(gs6)) (rspike hi lo xax if gname=="Korean"&cname=="UK", lcolor(gs6)), xtitle("Assumed Party Affiliation", size(medium) margin(small)) xlabel(0 " " 1 "Lab" 3 "LibDem" 5 "Cons" 6 " ") legend(order(1 "Irish" 2 "Korean") rows(1) position(6) region(margin(zero))) title("UK") ylabel(.1(.1).4) name(trkor_uk, replace) 

twoway (scatter mean xax if gname=="Irish"&cname=="USA", msymbol(circle) mcolor(gs6)) (scatter mean xax if gname=="Korean"&cname=="USA", msymbol(circle_hollow) mcolor(gs6)) (rspike hi lo xax if gname=="Irish"&cname=="USA", lcolor(gs6)) (rspike hi lo xax if gname=="Korean"&cname=="USA", lcolor(gs6)), xtitle(" ", size(medium) margin(small)) xlabel(0 " " 1 "Dem" 3 "Rep" 4 " ") legend(off) title("USA") ylabel(.1(.1).4) name(trkor_us, replace) 

grc1leg2 trkor_cnd trkor_uk trkor_us, xsize(7) ysize(2.5) rows(1) scale(1.8) ///
	imargin(small) graphregion(margin(zero)) plotregion(margin(zero)) ///
	legendfrom(trkor_uk) l1(Proportion, size(small)) title("Race/Ethnicity Treatment") 
graph export fig1_bottom.png

*==============================================================================
*	H1; figure 2
*==============================================================================

keep if sample==1

* save ttest results

capture drop order cname gname pname
egen order = seq(), f(1) t(6) 
gen cname = "Canada" in 1/6
	replace cname = "UK" in 7/12
	replace cname = "USA" in 13/16
gen gname = ""
gen pname = ""
foreach i in mean sd hi lo n {
	capture drop `i'
	gen `i' = .
	}

capture program drop mu1
program define mu1
	replace gname = "Others" in `1'
	replace mean = r(mu_1) in `1'
	replace sd = r(sd_1) in `1'
	replace n = r(N_1) in `1'
end
capture program drop mu2
program define mu2
	replace mean = r(mu_2) in `1'
	replace sd = r(sd_2) in `1'
	replace n = r(N_2) in `1'
end

	local i = 1
	ttest johnNdp_cnd, by(pid_ndp_cnd)
		replace pname = "NDP" in `i' 
		replace gname = "NDP" in `i'
		mu2 `i'
		local i = `i'+1
		replace pname = "NDP" in `i' 
		mu1 `i'
	local i = `i'+1
	ttest johnLib_cnd, by(pid_lib_cnd)
		replace pname = "Lib" in `i' 
		replace gname = "Lib" in `i'
		mu2 `i'
		local i = `i'+1
		replace pname = "Lib" in `i' 
		mu1 `i'
	local i = `i'+1
	ttest johnCons_cnd, by(pid_cons_cnd)
		replace pname = "Cons" in `i' 
		replace gname = "Cons" in `i'
		mu2 `i'
		local i = `i'+1
		replace pname = "Cons" in `i' 
		mu1 `i'
	local i = `i'+1
	ttest johnLab_uk, by(pid_lab_uk)
		replace pname = "Lab" in `i'
		replace gname = "Lab" in `i'
		mu2 `i'
		local i = `i'+1
		replace pname = "Lab" in `i'
		mu1 `i'
 	local i = `i'+1		
	ttest johnLibdem_uk, by(pid_libdem_uk)
		replace pname = "LibDem" in `i'
		replace gname = "LibDem" in `i'
		mu2 `i'
		local i = `i'+1
		replace pname = "LibDem" in `i'
		mu1 `i'		
	local i = `i'+1
	ttest johnCons_uk, by(pid_cons_uk)
		replace pname = "Cons" in `i' 
		replace gname = "Cons" in `i'
		mu2 `i'
		local i = `i'+1
		replace pname = "Cons" in `i' 
		mu1 `i'
	local i = `i'+1
	ttest johnDem_us, by(pid_dem_us)
		replace pname = "Dem" in `i'
		replace gname = "Dem" in `i'
		mu2 `i'
		local i = `i'+1
		replace pname = "Dem" in `i' 
		mu1 `i'
	local i = `i'+1
	ttest johnRep_us, by(pid_rep_us)
		replace pname = "Rep" in `i'
		replace gname = "Rep" in `i'
		mu2 `i'
		local i = `i'+1
		replace pname = "Rep" in `i' 
		mu1 `i'
replace hi = mean + invttail(n-1, 0.085)*(sd/sqrt(n))
replace lo = mean - invttail(n-1, 0.085)*(sd/sqrt(n))
		
*--------------------------------
* figure 2
*--------------------------------

capture drop xax
gen xax = order
	recode xax 1=0.85 2=1.15 3=2.85 4=3.15 5=4.85 6=5.15
twoway (scatter mean xax if gname!="Others"&cname=="Canada", msymbol(circle) mcolor(gs6)) (scatter mean xax if gname=="Others"&cname=="Canada", msymbol(circle_hollow) mcolor(gs6)) (rspike hi lo xax if gname!="Others"&cname=="Canada", lcolor(gs6)) (rspike hi lo xax if gname=="Others"&cname=="Canada", lcolor(gs6)), xlabel(0 " " 1 "NDP" 3 "Lib" 5 "Cons" 6 " ") xtitle(" ", size(medium) margin(small)) legend(off) ylabel(0(.1).6)  title(Canada) name(cnd, replace) 
	
twoway (scatter mean xax if gname!="Others"&cname=="UK", msymbol(circle) mcolor(gs6)) (scatter mean xax if gname=="Others"&cname=="UK", msymbol(circle_hollow) mcolor(gs6)) (rspike hi lo xax if gname!="Others"&cname=="UK", lcolor(gs6)) (rspike hi lo xax if gname=="Others"&cname=="UK", lcolor(gs6)), xlabel(0 " " 1 "Lab" 3 "LibDem" 5 "Cons" 6 " ") xtitle("Assumed Party Affiliation", size(medium) margin(small)) legend(order(1 "Co-partisans" 2 "Others") ring(0) rows(1) position(5) size(vsmall) region(margin(zero))) ylabel(0(.1).6) title(UK) name(uk, replace) 
	
twoway (scatter mean xax if gname!="Others"&cname=="USA", msymbol(circle) mcolor(gs6)) (scatter mean xax if gname=="Others"&cname=="USA", msymbol(circle_hollow) mcolor(gs6)) (rspike hi lo xax if gname!="Others"&cname=="USA", lcolor(gs6)) (rspike hi lo xax if gname=="Others"&cname=="USA", lcolor(gs6)), xlabel(0 " " 1 "Dem" 3 "Rep" 4 " ") xtitle(" ", size(medium) margin(small)) legend(off) ylabel(0(.1).6) title(USA) name(us, replace) 

grc1leg2 cnd uk us, scale(1.8) xsize(8) ysize(3.2) scheme(plotplain) row(1) imargin(small) graphregion(margin(zero)) plotregion(margin(zero)) legendfrom(uk) l1(Proportion, size(small))
graph export fig2.png


*==============================================================================
*	H2 & H3; figures 3 & 4
*==============================================================================

* generate variables indicating matched characteristics

foreach i in class race age male imm sch ideol {
	gen `i'Match = 0
}
	replace ageMatch = 1 if age<45 & age>34
		replace ageMatch = 1 if agecatUS == 4
	replace maleMatch = 1 if male == 1
	replace immMatch = 1 if immParent==1
	replace classMatch = 1 if johnMyclass==1
	replace raceMatch = 1 if johnMyeth==1
	replace schMatch = 1 if (tr_hc==1&pubSchool==0)|(tr_lc==1&pubSchool==1)
	replace ideolMatch = 1 if lrself==5&cntry!="US"
		replace ideolMatch = 1 if libcon==3
	bysort cntry: sum *Match

egen simScore = rowtotal(ageMatch maleMatch immMatch schMatch ideolMatch)

* store logit estimates

egen num = seq(), f(1) t(4) 
drop cname hi lo
gen cname = ""
foreach i in xval point hi lo {
	gen `i' = .
}

local k=1
foreach i in cnd uk us {
local cnd = "Canada"
local uk = "UK"
local us = "USA"
logit johnMyparty_`i' i.classMatch i.raceMatch i.ageMatch i.maleMatch i.immMatch i.schMatch i.ideolMatch
margins, at(classMatch=(0 1) raceMatch=(0 1)) level(83)
matrix A = r(table)
	forval x = 0/3 {
	replace cname = "``i''" in `k'
	replace xval = `x' in `k'
	local mx = `x'+1
	replace point = A[1,`mx'] in `k'
	replace lo = A[5,`mx'] in `k'
	replace hi = A[6,`mx'] in `k'
	local k = `k'+1
	}
}

*--------------------------------
* figure 3 
*--------------------------------

twoway (rspike hi lo xval) (scatter point xval, msymbol(circle)  mcolor(gs8)) if cname=="Canada", xlabel(-.3 " " 0 "None" 1 "Race" 2 "Class" 3 "Both" 3.3 " ", labsize(medium)) ylabel(.2(.1).6, labsize(medium)) xtitle("") ytitle("") title(Canada) legend(off) name(predcnd, replace)

twoway (rspike hi lo xval) (scatter point xval, msymbol(circle)  mcolor(gs8)) if cname=="UK", xlabel(-.3 " " 0 "None" 1 "Race" 2 "Class" 3 "Both" 3.3 " ", labsize(medium)) ylabel(.2(.1).6, labsize(medium)) xtitle("") ytitle("") title(UK) legend(off) name(preduk, replace)

twoway (rspike hi lo xval) (scatter point xval, msymbol(circle)  mcolor(gs8)) if cname=="USA", xlabel(-.3 " " 0 "None" 1 "Race" 2 "Class" 3 "Both" 3.3 " ", labsize(medium)) ylabel(.2(.1).6, labsize(medium)) xtitle("") ytitle("") title(USA) legend(off) name(predus, replace)

grc1leg2 predcnd preduk predus, ycommon scale(1.8) xsize(5.5) ysize(2) scheme(plotplain) row(1) imargin(small) graphregion(margin(zero)) plotregion(margin(zero)) l1("Predicted Probability", size(small)) loff 
graph export fig3.png
	
*--------------------------------
* figure 4
*--------------------------------

foreach i in cnd uk us {
	local cnd = "Canada"
	local uk = "UK"
	local us = "USA" 
	logit johnMyparty_`i' simScore 
		margins, at(simScore=(0(1)3))
		marginsplot, recast(scatter) recastci(rspike) title("``i''") xtitle("") ytitle("") level(83) legend(rows(1) position(6) region(margin(zero))) name(sims_`i', replace)
}

grc1leg2 sims_cnd sims_uk sims_us, ycommon scale(1.8) xsize(8) ysize(3.2) scheme(plotplain) row(1) imargin(small) graphregion(margin(zero)) plotregion(margin(zero)) l1("Predicted Probability", size(small)) b2title("Similarity Score", size(small)) loff 
graph export fig4.png

*==============================================================================
*	H4; online appendix tables C2 and D 
*==============================================================================

* restrict sample to partisans of Cons, Lab (UK), Cons, Lib, NDP (Canada), Dem Rep (US)

gen cproject_uk = 0 if cntry=="UK"&(pid_cons_uk==1|pid_lab_uk==1)
	replace cproject_uk = 1 if pid_cons_uk==1&johnLab_uk==1
	replace cproject_uk = 1 if pid_lab_uk==1&johnCons_uk==1

gen cproject_cnd = 0 if cntry=="Canada"&(pid_cons_cnd==1|pid_ndp_cnd==1)
	replace cproject_cnd = 1 if pid_ndp_cnd==1&johnCons_cnd==1
	replace cproject_cnd = 1 if pid_cons_cnd==1&johnNdp_cnd==1

gen cproject_us = 0 if cntry=="US"&(pid_dem_us==1|pid_rep_us==1)
	replace cproject_us = 1 if (pid_dem_us==1&johnRep_us==1)
	replace cproject_us = 1 if (pid_rep_us==1&johnDem_us==1)

* outgroup-ness

recode johnMyclass (0=1) (1=0), gen(johnNotMyclass)
recode johnMyeth (0=1) (1=0), gen(johnNotMyeth)

*--------------------------------
*	online appendix table C2
*--------------------------------

logit cproject_cnd johnNotMyclass johnNotMyeth
	est store cpro_cnd1
logit cproject_uk johnNotMyclass johnNotMyeth
	est store cpro_uk1
logit cproject_us johnNotMyclass johnNotMyeth
	est store cpro_us1

esttab cpro_cnd1 cpro_uk1  cpro_us1, b(%10.3f) se scalars(N r2_p "ll Log likelihood") star(* 0.05 ** 0.01) mtitle( "Canada" "UK" "US") 

*--------------------------------
*	online appendix table D
*--------------------------------

* logit using subsamples that exclude cases where counter-projection = stereotyping

gen cproject_cnd_sub = cproject_cnd 
	replace cproject_cnd_sub = . if (classR3 > classJohn3) & pid_cons_cnd==1
	replace cproject_cnd_sub = . if (classR3 < classJohn3) & pid_ndp_cnd==1
gen cproject_uk_sub = cproject_uk 
	replace cproject_uk_sub = . if (classR3 > classJohn3) & pid_cons_uk==1
	replace cproject_uk_sub = . if (classR3 < classJohn3) & pid_lab_uk==1
gen cproject_us_sub = cproject_us
	replace cproject_us_sub = . if (classR3 < classJohn3) & pid_3==1 // dem
	replace cproject_us_sub = . if (classR3 > classJohn3) & pid_3==3 // rep

logit cproject_cnd_sub johnNotMyclass johnNotMyeth 
	est store cpro_cndSub
logit cproject_us_sub johnNotMyclass johnNotMyeth
	est store cpro_usSub
logit cproject_uk_sub johnNotMyclass johnNotMyeth
	est store cpro_ukSub

esttab cpro_cnd1 cpro_cndSub cpro_uk1 cpro_ukSub cpro_us1 cpro_usSub, b(%10.3f) se scalars(N r2_p "ll Log likelihood") star(* 0.05 ** 0.01) mtitle("Canada" "Canada-Sub" "UK" "UK-Sub" "US" "US-Sub") compress label

*==============================================================================
*	other replication codes for online appendix
*==============================================================================

*--------------------------------
*	online appendix table B2
*--------------------------------

sum johnMyparty_* cproject_* 
bysort cntry: sum johnMyclass johnMyeth

*--------------------------------
* online appendix table C1
*--------------------------------

logit johnMyparty_cnd *Match 
	estimates store cnd1
logit johnMyparty_uk *Match 
	estimates store uk1
logit johnMyparty_us *Match 
	estimates store us1
	
logit johnMyparty_cnd simScore 
	estimates store cnd2
logit johnMyparty_uk simScore 
	estimates store uk2
logit johnMyparty_us simScore 
	estimates store us2
	
esttab  cnd1 uk1 us1 cnd2 uk2 us2, b(%10.3f) se scalars(N r2_p "ll Log likelihood") star(+ 0.10 * 0.05 ** 0.01) compress mtitle("Canada" "UK" "USA" "Canada" "UK" "USA") label

*\END
